﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;
using BLL.RF.Repair;
using Model.RF.Repair;

public partial class JointingCheck_JointingCheckAdd : System.Web.UI.Page
{
    private AssembleDetail amd;
    private AssembleDetails amds;
    private Repair rep;
    private RepairRoute rroute;
    private const string iso = "隔离器";
    private const string coup = "耦合板";
    private DataRow dr;
    private DataTable amddt
    {
        get
        {
            return (DataTable)ViewState["amddt"];
        }
        set
        {
            ViewState["amddt"] = value;
        }
    }

    #region 原来代码
    private JointingCheck jc;
    private ProductType_Flow pf;
    private List<JointingCheckInfo> list { get { return (List<JointingCheckInfo>)ViewState["checkinfolist"]; } set { ViewState["checkinfolist"] = value; } }

    public JointingCheck_JointingCheckAdd()
    {
        jc = new JointingCheck();
        amd = new AssembleDetail();
        amds = new AssembleDetails();
        pf = new ProductType_Flow();
        rep = new Repair();
        rroute = new RepairRoute();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            list = new List<JointingCheckInfo>();
            BindJointingFailure();

            amddt = new DataTable();
            amddt.Columns.Add(new DataColumn("BatchID", Type.GetType("System.String")));
            amddt.Columns.Add(new DataColumn("StuffTypeID", Type.GetType("System.String")));
            amddt.Columns.Add(new DataColumn("Remark", Type.GetType("System.String")));
        }
    }

    protected void BindJointingFailure()
    {
        JointingFailure jf = new JointingFailure();
        this.DropDownList1.DataSource = jf.JointingFailure_GetbyMan();
        this.DropDownList1.DataValueField = "FailureName";
        this.DropDownList1.DataTextField = "FailureName";
        this.DropDownList1.DataBind();
    }

    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        DataUpload();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        DataUpload();
    }

    private void DataUpload()
    {
        string productSN = this.TextBox1.Text.Trim();
        if (CommHelper.IsValidSN_forDataAcquisition(productSN))
        {
            productSN = CommHelper.GetProductSN(productSN);
            Add_JointingCheckInfo_tolist(productSN);
            foreach (JointingCheckInfo qci in list)
            {
                try
                {
                    jc.Insert(qci);
                }
                catch
                {
                    string str1 = "腔内焊点数据提交失败，请联系相关人员";
                    ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
                    return;
                }
            }

            Repair_AutoBuild(list);

            if (!WriteAssembleDetails(productSN))
            {
                string str = "隔离器与耦合板提交失败，请联系相关人员!";
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
                return;
            }

            string str2 = "提交成功";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str2 + "');</script>", false);
            list.Clear();
            this.Label1.Text = "";
            this.TextBox1.Text = "";
            this.TextBox1.Focus();
        }
        else
        {
            string str2 = "序列号不符合腔体序列号编码规则，或不是关联过的成品序列号，请检查!";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str2 + "');</script>", false);
        }
    }

    private void Repair_AutoBuild(List<JointingCheckInfo> list)
    {
        foreach (JointingCheckInfo qci in list)
        {
            if (qci.FailureName != "0.合格")
            {
                string repairPath = rroute.RepairRoute_GetbyStepItem("JointingStep")[0].RepairPath;
                rep.Repair_AutoBuild(qci.ProductSN, repairPath, qci.UserID);
                return;
            }
        }
    }

    protected void TextBox3_TextChanged(object sender, EventArgs e)
    {
        this.TextBox1.Focus();
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        string productSN = this.TextBox1.Text.Trim();
        if (CommHelper.IsValidSN_forDataAcquisition(productSN))
        {
            productSN = CommHelper.GetProductSN(productSN);
            Add_JointingCheckInfo_tolist(productSN);
        }
        else
        {
            string str2 = "序列号不符合腔体序列号编码规则，或不是关联过的成品序列号，请检查!";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str2 + "');</script>", false);
        }
    }

    private bool Add_JointingCheckInfo_tolist(string productSN)
    {
        string productTypeID = this.TextBox10.Text.Trim();
        string failureName = this.DropDownList1.SelectedValue;
        string responseUser = this.TextBox9.Text.Trim();
        if (failureName != "0.合格" && responseUser.Length != 6)
        {
            string str = "检验结果为不良时，需要录入相应责任人的六位工号，请确认！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
            return false;
        }
        if (failureName == "0.合格")
            responseUser = "";

        Page.Validate("VGroup1");
        if (Page.IsValid)
        {
            //20170728，之前的S代码是从物料装配录入那里取的，现在修改成在页面增加录入
            //MO m = new MO();
            //House h = new House();
            //string moID = h.LoadEntity(productSN).MOID;
            //if (string.IsNullOrEmpty(moID))
            //{
            //    string str = "不能提交，此腔体没有赋予对应的MO号，请反查物料装配工序数据";
            //    ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
            //    return false;
            //}
            //string productTypeID = m.LoadEntity(moID).ProductTypeID;
            ProductType p = new ProductType();
            if (string.IsNullOrEmpty(p.LoadEntity(productTypeID).ProductTypeID))
            {
                string str = "产品类型不存在或未登记过！";
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
                return false;
            }
            if (!pf.ProductType_Flow_HasValidSevenStep(productSN, productTypeID, "JointingStep"))
            {
                string str = "不能提交，请反查此腔体的七大管控工序的数据!";
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
                return false;
            }

            JointingCheckInfo qci = new JointingCheckInfo(productSN, DateTime.Now,productTypeID, User.Identity.Name, failureName, responseUser);
            list.Add(qci);
            this.TextBox9.Text = "";
            Displayliststring();
            return true;
        }
        return false;
    }

    private void Displayliststring()
    {
        string liststring = "";
        foreach (JointingCheckInfo qci in list)
        {
            liststring = liststring + qci.FailureName + "：" + qci.ResponseUser + "；　";
        }
        this.Label1.Text = liststring;
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        string productSN = this.TextBox2.Text.Trim();
        productSN = CommHelper.GetProductSN(productSN);
        this.GridView1.DataSource = jc.GetJointingChecksbySN(productSN);
        this.GridView1.DataBind();
    }
    #endregion

    /// <summary>
    /// 以下与隔离器、耦合板相关
    /// </summary>

    protected bool WriteAssembleDetails(string productSN)
    {
        if (amddt.Rows.Count > 0)
        {
            string ip = IPGet.getIp();
            DateTime dt = DateTime.Now;
            AssembleDetailInfo adi = new AssembleDetailInfo(productSN, dt, User.Identity.Name, ip);
            if (amd.AssembleDetailItems_Insert(adi, amddt))
            {
                amddt.Clear();
                BindISO();
                BindCOUP();
                return true;
            }
            else
            {
                return false;
            }
        }
        return true;
    }

    protected void BindISO()
    {
        this.GridView2.DataSource = amddt.Select("Remark = '" + iso + "'").Length == 0 ? null : amddt.Select("Remark = '" + iso + "'").CopyToDataTable();
        this.GridView2.DataBind();
    }

    protected void BindCOUP()
    {
        this.GridView3.DataSource = amddt.Select("Remark = '" + coup + "'").Length == 0 ? null : amddt.Select("Remark = '" + coup + "'").CopyToDataTable();
        this.GridView3.DataBind();
    }

    protected void TextBox4_TextChanged(object sender, EventArgs e)
    {
        this.TextBox5.Focus();
    }

    protected void TextBox5_TextChanged(object sender, EventArgs e)
    {
        Page.Validate("VGroup3");
        if (Page.IsValid)
        {
            dr = amddt.NewRow();
            dr["BatchID"] = this.TextBox4.Text.Trim();
            dr["StuffTypeID"] = this.TextBox5.Text.Trim();
            dr["Remark"] = iso;
            if (amddt.Select("BatchID = '" + this.TextBox4.Text.Trim() + "'").Length == 0)
            {
                amddt.Rows.InsertAt(dr, 0);
                BindISO();
                this.TextBox4.Text = "";
                this.TextBox5.Text = "";
                this.TextBox4.Focus();
            }
            else
            {
                string str = "已经在列表中";
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
            }
        }
    }

    protected void TextBox7_TextChanged(object sender, EventArgs e)
    {
        this.TextBox8.Focus();
    }

    protected void TextBox8_TextChanged(object sender, EventArgs e)
    {
        Page.Validate("VGroup4");
        if (Page.IsValid)
        {
            dr = amddt.NewRow();
            dr["BatchID"] = this.TextBox7.Text.Trim();
            dr["StuffTypeID"] = this.TextBox8.Text.Trim();
            dr["Remark"] = coup;
            if (amddt.Select("BatchID = '" + this.TextBox7.Text.Trim() + "'").Length == 0)
            {
                amddt.Rows.InsertAt(dr, 0);
                BindCOUP();
                this.TextBox7.Text = "";
                this.TextBox8.Text = "";
                this.TextBox7.Focus();
            }
            else
            {
                string str = "已经在列表中";
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
            }
        }
    }

    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((LinkButton)e.Row.Cells[3].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除!')");
            }
        }
    }

    protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        amddt.Select("BatchID = '" + GridView2.Rows[e.RowIndex].Cells[0].Text + "'")[0].Delete();
        BindISO();
    }

    protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((LinkButton)e.Row.Cells[3].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除!')");
            }
        }
    }

    protected void GridView3_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        amddt.Select("BatchID = '" + GridView3.Rows[e.RowIndex].Cells[0].Text + "'")[0].Delete();
        BindCOUP();
    }
}
